﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fis.Win.Utils;
using System.Data;

namespace Fis.Win.Rules
{
    public class GlobleViewProvider : ClientViewProvider
    {
        /// <summary>
        /// 是否包含未同步数据
        /// </summary>
        /// <returns></returns>
        public bool HasBillNoSync()
        {
            DataTableName = "TBILLMASTER";
            var count = DataContext.DefaultModel.Count(DataTableName, "SID",
                string.Format("DATEPART(month, FENTERDATE) = {0} AND FSYNC < 1", AppSession.Range));
            return count > 0;
        }

        /// <summary>
        /// 是否包含未同步数据
        /// </summary>
        /// <returns></returns>
        public bool HasInitBillNoSync()
        {
            DataTableName = "TBILLMASTER";
            var count = DataContext.DefaultModel.Count(DataTableName, "SID",
                string.Format("FENTERDATE < '{0}' AND FSYNC < 1", AppSession.Book["FBOOKSTARTIME"]));
            return count > 0;
        }

        /// <summary>
        /// 按期间同步
        /// </summary>
        /// <param name="range"></param>
        public bool BillSyncByRange(int range)
        {
            var param = AppSession.CreateServiceParamter(new System.Collections.Hashtable());
            param["RANGE"] = range;
            param["Action"] = "Range";

            var exec = InvokeService(param);

            if (exec)
            {
                System.Data.DataTable master = null, detail = null;
                if (ServiceProcess.Result.ContainsKey("master"))
                {
                    master = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["master"].ToString());
                    detail = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["detail"].ToString());

                    foreach (var d in master.AsEnumerable())
                    {
                        SaveBill(d, detail.Select(string.Format("FVCHID='{0}'", d["SID"])));
                    }
                }
            }

            Message = ServiceProcess.Message;
            return false;
        }

        /// <summary>
        /// 执行数据同步
        /// </summary>
        /// <param name="master"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool BillSync(DataRow m, DataRow[] d)
        {
            var param = AppSession.CreateServiceParamter(new System.Collections.Hashtable());
            param["master"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(new DataRow[] { m }.CopyToDataTable());
            param["detail"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(d.CopyToDataTable());
            param["Action"] = "AddNew";
            var exec = InvokeService(param);

            if (exec)
            {
                System.Data.DataTable master = null, detail = null;
                if (ServiceProcess.Result.ContainsKey("master"))
                {
                    master = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["master"].ToString());
                    detail = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["detail"].ToString());

                    foreach (var mr in master.AsEnumerable())
                    {
                        SaveBill(mr, detail.Select(string.Format("FVCHID='{0}'", mr["SID"])));
                    }
                }
            }

            Message = ServiceProcess.Message;
            return false;
        }

        /// <summary>
        /// 本地保存
        /// </summary>
        /// <param name="master"></param>
        /// <param name="detail"></param>
        public void SaveBill(DataRow master, DataRow[] detail)
        {
            var mda = new Agile.Engine.Data.DataAccess() { Context = DataContext, TableName = "TBILLMASTER" };
            var dda = new Agile.Engine.Data.DataAccess() { Context = DataContext, TableName = "TBILLDETAIL" };

            DataContext.DefaultModel.ExecuteTransaction(
                () =>
                {
                    mda.Delete(master["SID"].ToString());
                    foreach (var d in detail)
                        dda.Delete(d["SID"].ToString());

                    mda.AddNew(master);
                    dda.AddNew(detail);
                });
        }

    }
}
